package com.cskaoyan.order.service.impl;

import com.cskaoyan.order.biz.factory.OrderProcessPipelineFactory;
import com.cskaoyan.order.constant.OrderConstants;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dal.persistence.StockMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.remote.ProductApiClient;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: OrderQueryServiceImpl
 * @Description:
 * @Date: 2022/12/3 9:44
 * @author: mxy
 */

@Slf4j
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderProcessPipelineFactory orderProcessPipelineFactory;

    @Autowired
    StockMapper stockMapper;


    @Autowired
    OrderConverter orderConverter;

    @Autowired
    ProductApiClient productApiClient;


    /**
     * @descriptio: TODO  查询用户全部订单
     * @params: [request]
     * @return: com.cskaoyan.order.dto.OrderListResponse
     * @author: HeXiangZhi
     * @date: 2022/12/4 17:31
     */
    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        @NotNull @Min(1) Integer page = request.getPage();
        @NotNull @Min(1) Integer size = request.getSize();



        OrderListResponse orderListResponse = new OrderListResponse();
        List<OrderDetailInfo> orderDetailInfos = new ArrayList<>();

        Long userId = request.getUserId();
        Example example = new Example(Order.class);
        // example.setOrderByClause(request.getSort());
        // example.createCriteria()
        example.createCriteria().andEqualTo("userId",userId);
        List<Order> orders1 = orderMapper.selectByExample(example);
        orderListResponse.setTotal((long) orders1.size());
        // PageHelper.startPage(size*(page-1), size);
        PageHelper.startPage(page, size);
        // 按照创建时间降序
        example.setOrderByClause(OrderConstants.ORDER_SORT);
        List<Order> orders = orderMapper.selectByExample(example);

        for (Order order : orders) {
            OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);

            String orderId = order.getOrderId();
            List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
            orderDetailInfo.setOrderItemDto(orderItemDtos);

            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
            orderDetailInfo.setOrderShippingDto(orderShippingDto);

            orderDetailInfo.setUpdateTime(new Date());
            orderDetailInfos.add(orderDetailInfo);
        }

        orderListResponse.setData(orderDetailInfos);



        // PageHelper.clearPage();

        return orderListResponse;
    }

    /**
     * @descriptio: TODO   查询详细订单
     * @params: [orderId]
     * @return: com.cskaoyan.order.dto.OrderDetailResponse
     * @author: HeXiangZhi
     * @date: 2022/12/4 18:27
     */
    @Override
    public OrderDetailResponse orderDetail(String orderId) {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
        // 小问题：ordershipping中的地址是详细地址，不是全部地址
        OrderDetailResponse orderDetailResponse = orderConverter.order2res(order, orderShipping);

        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId",orderId);

        List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
        List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

        orderDetailResponse.setGoodsList(orderItemDtos);

        return orderDetailResponse;
    }
}
